﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Webcar.BLL;
using Webcar.Entitys;
using Webcar.Utils;
namespace Webcar.Controllers
{
    public partial class DriverController : BaseControler
    {
        public ActionResult Move(string man_code)
        {
            BS_USER_MOVE model = new BS_USER_MOVE();

            var user = WebcarDataProvider.DB.BS_EMP.Find(man_code);
            model.USER_NO = man_code;
            model.OLD_DEPT = user.DEPT_CODE;
            model.NEW_DEPT = user.DEPT_CODE;
            model.MOVE_DATE = DateTime.Now;
            model.OPERATOR = CurrentUser.GetLoginUserName();
            ViewBag.ManName = user.MAN_NAME;
            var dept = WebcarDataProvider.DB.sys_dept.Find(user.DEPT_CODE);
            ViewBag.DeptName = dept.deptname;
            return PartialView(model);
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult MoveSave(BS_USER_MOVE model)
        {
            try
            {
                CurrentUser.EnsurePermission(PermissionManager.调动车辆);
                string new_child_dept = this.Request.Form["NEW_CHILD_DEPT"];
                if (model.NEW_DEPT.IsNull())
                {
                    return Content("请选择新单位。");
                }
                if (new_child_dept.IsNull())
                {
                    return Content("请选择新下级单位。");
                }
                using (var DB = new WebcarContext())
                {
                    model.OPERATOR = CurrentUser.GetLoginUserName();
                    model.OPER_DATE = DateTime.Now;
                    model.MOVE_NO = "M" + DB.GetSearialId().ToString();


                    var user = DB.BS_EMP.Find(model.USER_NO);
                    if (user == null)
                    {
                        return Content("错误:未找到人员。");
                    }
                    if (user.DEPT_CODE == model.NEW_DEPT)
                    {
                        return Content("错误:调动单位不能与原单位相同。");
                    }
                    user.DEPT_CODE = model.NEW_DEPT;
                    user.CHILD_DEPT = new_child_dept;
                    DB.BS_USER_MOVE.Add(model);
                    DB.SaveChanges();
                }
                return SuccessAlertThenReload();
            }
            catch (Exception ex)
            {
                return Content("错误:" + ex.Message);
            }
        }
        public ActionResult MoveList()
        {
            DateTime start_date = DateTime.Now.AddMonths(-1);
            DateTime end_date = DateTime.Now;
            ViewBag.StartDate = start_date;
            ViewBag.EndDate = end_date;

            if (this.Request.Form["start_date"] != null && this.Request.Form["end_date"] != null)
            {
                start_date = this.Request.Form["start_date"].ToDateTime();
                end_date = this.Request.Form["end_date"].ToDateTime();
            }

            var list = from p in WebcarDataProvider.DB.BS_USER_MOVE
                       join p2 in WebcarDataProvider.DB.BS_EMP
                       on p.USER_NO equals p2.MAN_CODE
                       join p3 in WebcarDataProvider.DB.sys_dept
                       on p.NEW_DEPT equals p3.deptcode
                       join p4 in WebcarDataProvider.DB.sys_dept
                       on p.OLD_DEPT equals p4.deptcode
                       where p.MOVE_DATE >= start_date && p.MOVE_DATE <= end_date
                       select new
                       {
                           MoveNo = p.MOVE_NO,
                           ManName = p2.MAN_NAME,
                           NewDept = p3.deptname,
                           OldDept = p4.deptname,
                           MoveDate = p.MOVE_DATE,
                           OperDate = p.OPER_DATE,
                           Operator = p.OPERATOR,
                           Cause = p.CAUSE
                       };
            ViewBag.StartDate = start_date;
            ViewBag.EndDate = end_date;

            return View(model: list);
        }
    }
}